package com.hdoit.ares.web.cust.wechat;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.AdviceFeedback;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.IAdviceFeedbackService;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.enums.cust.AdviceStatus;
import com.hdoit.ares.selfcommon.utils.EmojiUtil;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.wechat.BaseWechat;

@Controller
@RequestMapping(value="/wechat/cust/adviceFeedback/")
public class AdviceFeedbackWechat extends BaseWechat{

	@Override
	public String getRoutePath() {
		
		return "areas/wechat/cust/adviceFeedback/";
	}

	private IAdviceFeedbackService adviceFeedbackService;
	
	public IAdviceFeedbackService getAdviceFeedbackService() {
		return adviceFeedbackService;
	}

	@Resource
	public void setAdviceFeedbackService(
			IAdviceFeedbackService adviceFeedbackService) {
		this.adviceFeedbackService = adviceFeedbackService;
	}

	private final String feedbackViewName = "feedback";
	private final String submitAdviceViewName = "submitAdvice";
	private final String feedbackListViewName = "feedbackList";
	

	@RequestMapping(value = feedbackViewName,method = { RequestMethod.GET, RequestMethod.POST})
	public String feedback(Model model){	
		return view(feedbackViewName);
	}
	
	@ResponseBody
	@RequestMapping(value = submitAdviceViewName,method = { RequestMethod.GET, RequestMethod.POST})
	public String submitAdvice(Model model,AdviceFeedback adviceFeedback){
		System.err.println(adviceFeedback);
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		
		adviceFeedback.setAdvice(EmojiUtil.emojiConverterToAlias(adviceFeedback.getAdvice()));
		adviceFeedback.setId(CommonUtils.getKeyId());
		adviceFeedback.setAdviceId(customer.getId());
		adviceFeedback.setCreateDate(DateUtils.getCurrent());
		adviceFeedback.setAdvicestate(AdviceStatus.Handling);
		adviceFeedback.setDeleteMark(YesOrNo.no);
		adviceFeedback.setModelStatus(ModelStatus.add);
		boolean result = adviceFeedbackService.applyChange(adviceFeedback);
		
		return String.valueOf(result);
	}
	
	@RequestMapping(value = feedbackListViewName,method = { RequestMethod.GET, RequestMethod.POST})
	public String feedbackList(Model model){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer  = (Customer)loginModel.get(AresManageContainer.customer_account_info);
		List<AdviceFeedback> adviceFeedbacks = adviceFeedbackService.queryListWechat(customer.getId());
		
		for (AdviceFeedback adviceFeedback : adviceFeedbacks) {
			adviceFeedback.setAdvice(EmojiUtil.emojiConverterUnicodeStr(adviceFeedback.getAdvice()));
		}

		model.addAttribute("adviceFeedbacks", adviceFeedbacks);
		return view(feedbackListViewName);
	}
	
}
